package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LibraryFines extends Activity implements OnClickListener {

	TextView patronNo;
	TextView name;
	TextView status;
	TextView expDate;
	TextView message;
	Button showPaymentHistory;
	String patronNoValue,nameValue,statusValue,expDateValue,messageValue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_library_fines);
		final Intent i = new Intent(this, FinesResult.class);
		patronNo = (TextView) findViewById(R.id.txtPatrron);
		name = (TextView) findViewById(R.id.txtName);
		status = (TextView) findViewById(R.id.txtStatus);
		expDate = (TextView) findViewById(R.id.txtexpDate);
		message = (TextView) findViewById(R.id.FinesStatus);
		showPaymentHistory = (Button) findViewById(R.id.btnShowPaymentHistory);
		showPaymentHistory.setOnClickListener(this);

		Intent intename = getIntent();
		patronNoValue = intename.getStringExtra("patronNo");
		nameValue = intename.getStringExtra("Name");
		statusValue = intename.getStringExtra("Status");
		expDateValue = intename.getStringExtra("expDate");
		messageValue = intename.getStringExtra("message");

		patronNo.setText(patronNoValue);
		name.setText(nameValue);
		status.setText(statusValue);
		expDate.setText(expDateValue);
		message.setText(messageValue);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.library_fines, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == showPaymentHistory) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LibraryFineMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("message", "/html[1]/body[1]/p[1]---");
			request.setXpathMap(xpathMap);

			// submit=Show+Payment+History&Action=ShowPaymentHistory
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("submit", "Show+Payment+History");
			parametersMap.put("Action", "ShowPaymentHistory");
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String messageValue = respones.getResultsMap().get("message");
			if(messageValue.equalsIgnoreCase("You do not have any library fine payment history!")){
				
				Intent i = new Intent(this, NoPaymentHistory.class);
				i.putExtra("message", messageValue);
				i.putExtra("patronNo", patronNoValue);
				i.putExtra("name", nameValue);
				i.putExtra("status", statusValue);
				i.putExtra("expDate", expDateValue);
				startActivity(i);
			}
			else{
				
				BPEL_Process bpelProcess2 = new BPEL_Process();
				BPEL_Request request2 = new BPEL_Request();
				request2.setUrl("http://webspace1.apiit.lk/gims/library/LibraryFineMain.asp");
				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("message", "/html[1]/body[1]/table[2]---");
				request2.setXpathMap(xpathMap2);

				// submit=Show+Payment+History&Action=ShowPaymentHistory
				Map<String, String> parametersMap2 = new HashMap<String, String>();
				parametersMap2.put("submit", "Show+Payment+History");
				parametersMap2.put("Action", "ShowPaymentHistory");
				request2.setParametersMap(parametersMap2);

				request2.setType("POST");
				BPEL_Response response2 = null;
				try {
					response2 = bpelProcess2.doRequest(request2);

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				String testResult =response2.getResultsMap().get("message").replaceAll("Payment History", "").replaceAll("Date", "").replaceAll("Amount Paid", "").replaceAll("&nbsp", "");
						//respones.getResultsMap().get("message").replaceAll("Payment History", "").replaceAll("Date", "").replaceAll("Amount Paid", "").replaceAll("&nbsp", "").replaceAll("/t", "").replaceAll("/n", "");
				String result [] = testResult.split(";");
				//String patronNumber = result[3];
				
				Toast.makeText(getApplicationContext(), testResult,
						Toast.LENGTH_LONG).show();
				
				Intent i = new Intent(this, PaymentHistory.class);
				i.putExtra("array",result);
				startActivity(i);
			}
			

		}

	}

}
